//
//  MainViewController.swift
//  SinaWeibo
//
//  Created by 许良 on 16/6/1.
//  Copyright © 2016年 fading. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //tabBar.tintColor = UIColor.orangeColor()
        addChildViewControllers()
   
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //加号
        setupComposeBtn()
    }
    
    
    private func setupComposeBtn() {
        
        tabBar.addSubview(composeBtn)
        let width = UIScreen.mainScreen().bounds.size.width / CGFloat((viewControllers?.count)!)
        let rect = CGRectMake(0, 0, width, 49)
        composeBtn.frame = CGRectOffset(rect, 2 * width, 0)
    }
    
    func composeBtnClick () {
        
    }
    
    
    private func addChildViewControllers() {
        addChildViewController(HomeViewController(),  title: "首页",  imageNamed: "tabbar_home")
        addChildViewController(MessageViewController(),  title: "消息", imageNamed: "tabbar_message_center")
        addChildViewController(NULLViewController(),  title: "", imageNamed: "")
        addChildViewController(DiscoverViewController(), title: "发现", imageNamed: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "个人", imageNamed: "tabbar_profile")

    }
    
    private func addChildViewController(childController: UIViewController, title: String, imageNamed: String) {
        
        childController.tabBarItem.image = UIImage(named: imageNamed)
        childController.tabBarItem.selectedImage = UIImage(named: imageNamed + "_highlighted")
        
        childController.title = title
        let nav = UINavigationController()
        nav.addChildViewController(childController)
        addChildViewController(nav)
    }
    
    // MARK : -懒加载ann
    private lazy var composeBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        
        //添加加号按钮监听
        btn.addTarget(self, action: #selector(MainViewController.composeBtnClick), forControlEvents: .TouchUpInside)
        return btn
    }()
    
}
